package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pro.mapper.WorkFlowProcessMapper;
import com.qidianit.mes.pro.service.WorkFlowProcessService;

import com.qidianit.mes.pro.vo.WorkflowProcessRelation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @Author: shanzhe
 * @Date: 2025/9/18 - 19:10
 */
@Service
public class WorkFlowProcessServiceImpl implements WorkFlowProcessService {
    @Autowired
    private WorkFlowProcessMapper workFlowProcessMapper;

    @Override
    public PageInfo<WorkflowProcessRelation> queryProcessCompositionsByWorkflowIdWithPaging(Integer page, Integer size, Integer workflowId) {
        PageHelper.startPage(page, size);
        List<WorkflowProcessRelation> workflowProcessRelationList = workFlowProcessMapper.queryProcessCompositionsByWorkflowIdWithPaging(page, size, workflowId);
        PageInfo<WorkflowProcessRelation> workflowProcessRelationPageInfo = new PageInfo<>(workflowProcessRelationList);
        return workflowProcessRelationPageInfo;
    }
    /*
    新增工序组成
     */
    @Override
    public int addWorkflowProcessRelation(WorkflowProcessRelation workflowProcessRelation) {
        List<WorkflowProcessRelation> workflowProcessRelationList = workFlowProcessMapper.queryProcessCompositionsByWorkflowId(workflowProcessRelation.getWorkflowId());
        if(!workflowProcessRelationList.isEmpty()){
            for (WorkflowProcessRelation processRelation : workflowProcessRelationList) {
                if(processRelation.getProcessId().equals(workflowProcessRelation.getProcessId())){
                    return -1;
                }
                if(processRelation.getStepOrder().equals(workflowProcessRelation.getStepOrder())){
                    return -2;
                }
            }
        }
        return workFlowProcessMapper.addWorkflowProcessRelation(workflowProcessRelation);
    }

    @Override
    public WorkflowProcessRelation getWorkflowProcessRelationById(Integer id) {
        return workFlowProcessMapper.getWorkflowProcessRelationById(id);
    }

    @Override
    public int updateWorkflowProcessRelation(WorkflowProcessRelation workflowProcessRelation) {
        List<WorkflowProcessRelation> workflowProcessRelationList = workFlowProcessMapper.queryProcessCompositionsByWorkflowId(workflowProcessRelation.getWorkflowId());
        if(!workflowProcessRelationList.isEmpty()){
            for (WorkflowProcessRelation processRelation : workflowProcessRelationList) {
                if(!processRelation.getId().equals(workflowProcessRelation.getId())){
                if(processRelation.getProcessId().equals(workflowProcessRelation.getProcessId())){
                    return -1;
                }
                if(processRelation.getStepOrder().equals(workflowProcessRelation.getStepOrder())){
                    return -2;
                }
            }
            }
        }
        return workFlowProcessMapper.updateWorkflowProcessRelation(workflowProcessRelation);
    }

    @Override
    public int deleteWorkflowProcessRelationById(Integer id) {
        return workFlowProcessMapper.deleteWorkflowProcessRelationById(id);
    }
    /*
 批量删除工艺的工序组成
  */
    @Override
    public int deleteWorkflowProcessRelationByIds(List<Integer> ids) {
        Integer sum = 0;
        for (Integer id : ids) {
            sum += workFlowProcessMapper.deleteWorkflowProcessRelationById(id);
        }
        if (sum.equals(ids.size())) {
            return 1;
        }
        if (sum > 0) {
            return -1;
        }
        return 0;
    }
}
